package dem1;

//import static dem1.Test.shell;

import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-05-06
 * Time: 13:16
 */
public class Test2 {

    public static void insertSort(int[] array){
        for(int i = 1; i < array.length; i++){
            int j = i - 1;
            int tmp = array[i];
            for (; j >= 0; j--){
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


        // 希尔排序
    public static void shellSort(int[] array){
        int length = array.length;
        while (length > 1){
            length /= 2;
            shell(array, length);
        }
    }

    public static void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++){
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j -= gap){
                if (array[j] > tmp){
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    private static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    // 选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minindex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minindex]){
                    minindex = j;
                }
            }
            swap(array, i, minindex);
        }
    }


// 堆排序
    public static void heapSort(int[] array){
        for (int i = (array.length - 2) / 2; i >= 0; i--) {
            shiftdown(array,i);
        }
    }
    public static void shiftdown(int[] array, int parant){
        int child = 2 * parant + 1;
        int length = array.length;
        while (child < length){
            if (child + 1 < length && array[child] < array[child + 1]){
                child = child + 1;
            }
            if (array[parant] < array[child]){
                swap(array,parant,child);
                parant = child;
                child = 2 * parant + 1;
            }else {
                break;
            }
        }
    }

    // 冒泡排序
    private static void bubbleSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if (!flag){
                return;
            }
        }
    }

// 快速
    public static void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }

    private static int threenum(int[] array, int begin, int end){
        int mid = (begin + end) / 2;
        if (array[begin] < array[end]){
            if (array[mid] < array[begin]){
                return begin;
            }else if(array[mid] > array[end]){
                return end;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[end]){
                return end;
            }else if (array[mid] > array[begin]){
                return begin;
            }else {
                return mid;
            }
        }
    }

    public static void quick(int[] array, int begin, int end){
        if (begin > end){
            return;
        }
        int tmp = threenum(array,begin,end);
        swap(array,begin,tmp);
        int index = find2(array,begin,end);
        quick(array,begin,index - 1);
        quick(array,index + 1,end);
    }

    public static int find(int[] array, int begin, int end){
        int i = begin;
        while (begin < end){
            while (begin < end && array[end] >= array[i]){
                end--;
            }
            while (begin < end && array[begin] <= array[i]){
                begin++;
            }
            swap(array,end,begin);
        }
        swap(array,begin,i);
        return begin;
    }

    public static int find1(int[] array, int begin, int end){
        int tmp = array[begin];
        while (begin < end){
            while (begin < end && array[end] >= tmp){
                end--;
            }
            array[begin] = array[end];
            while (begin < end && array[begin] <= tmp){
                begin++;
            }
            array[end] = array[begin];
        }
        array[begin] = tmp;
        return begin;
    }

    public static int find2(int[] array, int begin, int end){
        int prev = begin;
        int cur = prev + 1;
        while (cur <= end){
            if (array[cur] < array[begin] && array[++prev] != array[cur]){
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,begin,prev);
        return prev;
    }

    // 归并排序---递归
    public static void mergeSort(int[] array){
        // write code  here
        //mergeSortFunc1(array,0,array.length - 1);
        mergeSortFunc(array);
    }

    private static void mergeSortFunc1(int[] array, int begin, int end){
        if (begin >= end){
            return;
        }
        int mid = (end + begin) / 2;
        mergeSortFunc1(array, begin, mid);
        mergeSortFunc1(array, mid + 1, end);
        merge(array, begin, mid, end);
    }

    //归并排序-非递归.
    private static void mergeSortFunc(int[] array){
        int gap = 1;
        int left = 0;
        int right = 0;
        int mid = 0;
        while (gap < array.length){
            for (int i = 0; i < array.length; i += 2 * gap) {
                left = i;
                mid = left + gap - 1;
                right = mid + gap;
                if (right >= array.length){
                    right = array.length - 1;
                }
                if (mid >= array.length){
                    mid = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    private static void merge(int[] array, int begin, int mid, int end){
        int[] arr = new int[end - begin + 1];
        int s1 = begin;
        int s2 = mid + 1;
        int k = 0;
        while (s1 <= mid && s2 <= end){
            if (array[s1] < array[s2]){
                arr[k++] = array[s1++];
            }else {
                arr[k++] = array[s2++];
            }
        }
        while (s2 <= end){
                arr[k++] = array[s2++];
        }
        while (s1 <= mid){
                arr[k++] = array[s1++];
        }
        for (int i = 0; i < k; i++) {
            array[i + begin] = arr[i];
        }
    }

// 计数排序
    public static void countSort(int[] array){
        int max = array[0];
        int min = max;
        int len = array.length;
        for (int i = 0; i < len; i++) {
            if (array[i] > max){
                max = array[i];
                continue;
            }
            if (array[i] < min){
                min = array[i];
            }
        }
        int[] arr = new int[max - min + 1];
        for (int i = 0; i < len; i++) {
            arr[array[i] - min]++;
        }
        int k = 0;
        for (int i = 0; i < arr.length; i++) {
            int n = arr[i];
            while (n > 0){
                array[k] = i + min;
                k++;
                n--;
            }
        }
    }


    public static void main(String[] args) {
        //int[] arr = {10,20,8,25,35,6,18,30,5,15,28};
        //insertSort(arr);
        //selectSort(arr);
        int[] arr = {5,2,3,1};
        //quickSort(arr);
        //heapSort(arr);
        //bubbleSort(arr);
        //mergeSort(arr);
        //countSort(arr);
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
